package graphql

func EndpointAvgLatency(serviceName string, endpointName string, start string, end string) (map[int64]any, error) {
	condition, duration := GetCondition(serviceName, endpointName, start, end, "endpoint_avg")
	latencies, err := GetTimeseriesMetric(condition, duration)
	return transfromMillSecondToNanoSecond(latencies), err
}

func EndpointCps(serviceName string, endpointName string, start string, end string) (map[int64]any, error) {
	condition, duration := GetCondition(serviceName, endpointName, start, end, "endpoint_cpm")
	throughputs, err := GetTimeseriesMetric(condition, duration)
	return transfromCpmToCps(throughputs), err
}

var PercentilesLabels = map[string]string{
	"0": "0.5000000000",
	"1": "0.7500000000",
	"2": "0.9000000000",
	"3": "0.9500000000",
	"4": "0.9900000000",
}

func EndpointPercentiles(serviceName string, endpointName string, start string, end string) (map[string]map[int64]any, error) {
	condition, duration := GetCondition(serviceName, endpointName, start, end, "endpoint_percentile")
	percentiles, err := GetLabeldTimeseriesMetric(condition, duration, []string{"0", "1", "2", "3", "4"})
	return percentiles, err
}

func transfromCpmToCps(throughputs map[int64]any) map[int64]any {
	for ts, cpm := range throughputs {
		throughputs[ts] = (float64)(cpm.(int64)) / float64(60)
	}
	return throughputs
}

func transfromMillSecondToNanoSecond(avgLatency map[int64]any) map[int64]any {
	for ts, cpm := range avgLatency {
		avgLatency[ts] = (float64)(cpm.(int64)) * 1e6
	}
	return avgLatency
}
